import { createStore } from 'vuex'

//购物车本地保存
const setCartLocalStorage = (state) => {
  const { cartList } = state;
  const cartListSting = JSON.stringify(cartList);
  localStorage.setItem('cartList', cartListSting);
}
const getCartLocalStorage = () => {
  const cartList = JSON.parse(localStorage.getItem('cartList')) || {};
  return cartList
}

//地址本地保存
// const setAddressLocalStorage = (state) => {
//   const { addRessList } = state;
//   const addRessListString = JSON.stringify(addRessList)
//   localStorage.setItem('addressList', addRessListString)
// }
// const getAddressLocalStorage = () => {
//   const addressList = JSON.parse(localStorage.getItem('addressList')) || {};
//   return addressList;
// }
export default createStore({
  state: {
    cartList: getCartLocalStorage(),
    // cartList: {
    //   //  shopId:{
    //   //   shopName:'',
    //   //   shopList:{
    //   //     productId:{}
    //   //   }
    //   //  }
    // }
    // addRessList: getAddressLocalStorage()
  },
  getters: {
  },
  mutations: {
    //添加商品数据
    changeItem(state, payload) {
      const { shopId, productId, item, num, name } = payload;
      if (num !== -1) {
        //创建商铺
        if (!state.cartList[shopId]) {
          state.cartList[shopId] = {}
        }
        //商铺名字
        if (!state.cartList[shopId].shopName) {
          state.cartList[shopId].shopName = name;
        }
        //商铺ID
        if (!state.cartList[shopId].shopId) {
          state.cartList[shopId].shopId = shopId
        }
        //商品信息
        if (!state.cartList[shopId].productList) {
          state.cartList[shopId].productList = {}
        }
        //添加商品信息
        if (!state.cartList[shopId].productList[productId]) {
          state.cartList[shopId].productList[productId] = item;
          state.cartList[shopId].productList[productId].count = 0;
        }
      }
      //商品总数
      state.cartList[shopId].productList[productId].count += num;
      //商品选中状态
      if (state.cartList[shopId].productList[productId].count > 0) {
        state.cartList[shopId].productList[productId].check = true;
      }
      //商品数量为0删除商品数据
      if (state.cartList[shopId].productList[productId].count <= 0 && num === -1) {
        delete state.cartList[shopId].productList[productId]
        if (JSON.stringify(state.cartList[shopId].productList) === '{}') {
          delete state.cartList[shopId]
        }
      }
      //本地存储 
      setCartLocalStorage(state)
    },
    //小圆点点击切换
    checkChange(state, payload) {
      const { shopId, productId } = payload;
      state.cartList[shopId].productList[productId].check = !state.cartList[shopId].productList[productId].check
      setCartLocalStorage(state)
    },
    //全选按钮
    allChange(state, payload) {
      const { id, status } = payload;
      const { chechAllChange } = status.value
      for (const k in state.cartList[id].productList) {
        state.cartList[id].productList[k].check = !chechAllChange
      }
      setCartLocalStorage(state)
    },
    //清除购物车
    clearCart(state, shopId) {
      delete state.cartList[shopId];
      setCartLocalStorage(state)
    },
    //添加地址数据
    // setAddressList(state, payload) {
    //   const { num,
    //     city,
    //     community,
    //     doorplate,
    //     name,
    //     phone } = payload;
    //   if (!state.addRessList[num]) {
    //     state.addRessList[num] = {}
    //   }
    //   state.addRessList[num].city = city.value;
    //   state.addRessList[num].community = community.value
    //   state.addRessList[num].doorplate = doorplate.value
    //   state.addRessList[num].name = name.value
    //   state.addRessList[num].phone = phone.value
    //   state.addRessList[num]._id = num;
    //   setAddressLocalStorage(state);
    // },
    // //更新地址数据
    // updateAddressList(state, payload) {
    //   const { id, city, community, doorplate, name, phone } = payload
    //   let add = state.addRessList[id]
    //   add.city = city;
    //   add.community = community
    //   add.doorplate = doorplate
    //   add.name = name
    //   add.phone = phone
    //   state.addRessList[id] = add;
    //   setAddressLocalStorage(state);
    // }
  },
  actions: {
  },
  modules: {
  }
})
